package com.totcy.atom.view.activity;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.totcy.atom.R;
import com.totcy.atom.adapter.QuestionPagerAdapter;
import com.totcy.atom.bean.QuestionBean;
import com.totcy.atom.presenter.QuestionPresenter;
import com.totcy.atom.presenter.contract.QuestionContract;
import com.totcy.baselibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 题目界面
 */
public class QuestionActivity extends BaseFullActivity<QuestionContract.View, QuestionPresenter> implements QuestionContract.View, ViewPager.OnPageChangeListener {

    //<img src=\"images/f1db9867f84caef11c34f104f8505c32.png\" alt=\"f1db9867f84caef11c34f104f8505c32.png\" />"
    //http://rk.isoft365.com/rkimages/ff5b34752ee34d917a0eb912af97754a.png

    @BindView(R.id.vp_question)
    ViewPager viewPager;
    @BindView(R.id.tv_questionProgress)
    TextView tvQuestionProgress;
    @BindView(R.id.tv_questionCategory)
    TextView tvQuestionCategory;

    private List<QuestionBean> questions = new ArrayList<>();
    private QuestionPagerAdapter pagerAdapter;
    private int type;

    @Override
    public String getTitleName() {
        return getIntent().getStringExtra("date");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_question;
    }

    @Override
    public void initView() {
        type = getIntent().getIntExtra("type", 0);

        viewPager.setAdapter(pagerAdapter = new QuestionPagerAdapter(this, questions, type));
        viewPager.addOnPageChangeListener(this);

        initData();
    }

    private void initData() {
        String date = getIntent().getStringExtra("date");
        String subject = getIntent().getStringExtra("subject");
        showProgress("");
        getPresenter().getQuestionList(subject, date, type);
    }

    @OnClick({R.id.btn_lastQuestion, R.id.btn_lookAnswer, R.id.btn_lookAllQuestion, R.id.btn_nextQuestion})
    public void viewOnClick(View v) {
        switch (v.getId()) {

            case R.id.btn_lastQuestion:
                int lastPosition = viewPager.getCurrentItem() - 1;
                if (lastPosition >= 0) {
                    viewPager.setCurrentItem(lastPosition);
                }
                break;
            case R.id.btn_lookAnswer:
                //查看答案
                questions.get(viewPager.getCurrentItem()).setShowAnswer(true);
                pagerAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_lookAllQuestion:
                ToastUtil.showToast(this, "未实现，使用左右滑动查看吧！");
                break;
            case R.id.btn_nextQuestion:
                int nextPosition = viewPager.getCurrentItem() + 1;
                if (nextPosition < questions.size()) {
                    viewPager.setCurrentItem(nextPosition);
                }
                break;

        }
    }

    @Override
    public void onQuestionList(List<QuestionBean> questions) {
        dismissProgress();
        this.questions.clear();
        this.questions.addAll(questions);
        pagerAdapter.notifyDataSetChanged();

        tvQuestionProgress.setText(String.format(Locale.CHINA, "题目：%d/%d", 1, questions.size()));
        if (questions.get(0).getCategory() != null) {
            tvQuestionCategory.setText(String.format("分类：%s", questions.get(0).getCategory().getName()));
        }
    }

    @Override
    public void onFail(int code, String message) {
        ToastUtil.showToast(this, message);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        tvQuestionProgress.setText(String.format(Locale.CHINA, "题目：%d/%d", i + 1, questions.size()));
        if (questions.get(i).getCategory() != null) {
            tvQuestionCategory.setText(String.format("分类：%s", questions.get(i).getCategory().getName()));
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}
